/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import l1j.server.server.model.map.L1Map;
import l1j.server.server.model.map.L1WorldMap;
import l1j.server.server.types.Point;
import l1j.server.server.utils.Random;

public class L1Location extends Point {
	/**
	 * 取回隨機傳送座標
	 *
	 * @param baseLocation 自己的L1Location
	 * @param min 範圍最小質
	 * @param max 範圍最大質
	 * @param isRandomTeleport 是否閃避小屋 與戰爭區域座標
	 * @return 新的Location
	 */
	public static L1Location randomLocation(final L1Location baseLocation, int min,
			final int max, final boolean isRandomTeleport) {
		L1Location newLocation = null;
		if (min > max) {
			throw new IllegalArgumentException("min > max 設定異常");
		}
		if (max <= 0) {
			return new L1Location(baseLocation);
		}
		if (min < 0) {
			min = 0;
		}

		newLocation = new L1Location();
		int newX = 0;
		int newY = 0;
		final int locX = baseLocation.getX();
		final int locY = baseLocation.getY();
		final short mapId = (short) baseLocation.getMapId();
		final L1Map map = baseLocation.getMap();

		newLocation.setMap(map);

		int locX1 = locX - max;
		int locX2 = locX + max;
		int locY1 = locY - max;
		int locY2 = locY + max;

		// map範囲
		final int mapX1 = map.getX();
		final int mapX2 = mapX1 + map.getWidth();
		final int mapY1 = map.getY();
		final int mapY2 = mapY1 + map.getHeight();

		// 最大でもマップの範囲内までに補正
		if (locX1 < mapX1) {
			locX1 = mapX1;
		}
		if (locX2 > mapX2) {
			locX2 = mapX2;
		}
		if (locY1 < mapY1) {
			locY1 = mapY1;
		}
		if (locY2 > mapY2) {
			locY2 = mapY2;
		}

		final int diffX = locX2 - locX1; // x方向
		final int diffY = locY2 - locY1; // y方向

		int trial = 0;
		// 試行回数を範囲最小値によってあげる為の計算
		final int amax = (int) Math.pow(1 + max * 2, 2);
		final int amin = min == 0 ? 0 : (int) Math.pow(1 + (min - 1) * 2, 2);
		final int trialLimit = 40 * amax / (amax - amin);

		boolean run = true;
		while (run) {
			if (trial >= trialLimit) {
				newLocation.set(locX, locY);
				run = false;
				break;
			}
			trial++;

			try {
				newX = locX1 + Random.nextInt(diffX + 1);
				newY = locY1 + Random.nextInt(diffY + 1);
			} catch (final Exception e) {
				newLocation.set(locX, locY);
				run = false;
				break;
			}

			newLocation.set(newX, newY);

			if (baseLocation.getTileLineDistance(newLocation) < min) {
				continue;

			}
			if (isRandomTeleport) { // 閃避小屋 與戰爭區域座標
				// 戰爭區域
				if (L1CastleLocation.checkInAllWarArea(newX, newY, mapId)) {
					continue;
				}

				// 小屋內座標
				if (L1HouseLocation.isInHouse(newX, newY, mapId)) {
					continue;
				}
			}
			// 位置可通行中斷線程
			if (map.isInMap(newX, newY) && map.isPassable(newX, newY)) {
				run = false;
				break;
			}
		}
		return newLocation;
	}

	protected L1Map _map = L1Map.newNull();

	public L1Location() {
		super();
	}

	public L1Location(final int x, final int y, final int mapId) {
		super(x, y);
		this.setMap(mapId);
	}

	public L1Location(final int x, final int y, final L1Map map) {
		super(x, y);
		this._map = map;
	}

	public L1Location(final L1Location loc) {
		this(loc._x, loc._y, loc._map);
	}

	public L1Location(final Point pt, final int mapId) {
		super(pt);
		this.setMap(mapId);
	}

	public L1Location(final Point pt, final L1Map map) {
		super(pt);
		this._map = map;
	}

	@Override
	public boolean equals(final Object obj) {
		if (!(obj instanceof L1Location)) {
			return false;
		}
		final L1Location loc = (L1Location) obj;
		return this.getMap() == loc.getMap() && this.getX() == loc.getX()
				&& this.getY() == loc.getY();
	}

	public L1Map getMap() {
		return this._map;
	}

	public int getMapId() {
		return this._map.getId();
	}

	@Override
	public int hashCode() {
		return 7 * this._map.getId() + super.hashCode();
	}

	/**
	 * 返回此座標可移動隨機範圍的位置。
	 *
	 * @param max 最大範圍質
	 * @param isRandomTeleport 是否閃避城區 小屋區
	 * @return 新座標
	 */
	public L1Location randomLocation(final int max, final boolean isRandomTeleport) {
		return this.randomLocation(0, max, isRandomTeleport);
	}

	/**
	 * 取回隨機傳送座標
	 *
	 * @param min 範圍最小質
	 * @param max 範圍最大質
	 * @param isRandomTeleport 是否閃避小屋 與戰爭區域座標
	 * @return 新的Location
	 */
	public L1Location randomLocation(final int min, final int max, final boolean isRandomTeleport) {
		return L1Location.randomLocation(this, min, max, isRandomTeleport);
	}

	public void set(final int x, final int y, final int mapId) {
		this.set(x, y);
		this.setMap(mapId);
	}

	public void set(final int x, final int y, final L1Map map) {
		this.set(x, y);
		this._map = map;
	}

	public void set(final L1Location loc) {
		if(loc._x !=0 || loc._y!=0){
		this._map = loc._map;
		this._x = loc._x;
		this._y = loc._y;
		}
	}

	public void set(final Point pt, final int mapId) {
		this.set(pt);
		this.setMap(mapId);
	}

	public void set(final Point pt, final L1Map map) {
		this.set(pt);
		this._map = map;
	}

	public void setMap(final int mapId) {
		this._map = L1WorldMap.getInstance().getMap((short) mapId);
	}

	public void setMap(final L1Map map) {
		this._map = map;
	}

	@Override
	public String toString() {
		return String.format("(%d, %d) on %d", this._x, this._y, this._map.getId());
	}
}
